package com.chrisevansdev.irisysaudit;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

public class AuditConfig extends Activity implements OnClickListener {
	
	private int mHour;
    private int mMinute;
	static final int TIME_DIALOG_ID = 0;
	ArrayList<String> checkoutArray = new ArrayList<String> ();
	
	//------------------------------------------------------
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.audit_config);
				
		// get the current time
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        // display the current date
        updateDisplay();
	}

	//------------------------------------------------------
	
    public void onClick(View view) {
    	
    	switch (view.getId()) {
    	
    	case R.id.btnAddCheckout:
    		    		
    		// get the text from the checkout edit box
    		EditText txtCheckout = (EditText) findViewById(R.id.txtCheckout);
    		String strCheckout = txtCheckout.getText().toString();    		
    		
    		// do nothing if the checkout text is empty
    		if (strCheckout.isEmpty()) return;
    		
    		// get the group to which we want to add
    		LinearLayout checkoutGroup = (LinearLayout)findViewById(R.id.linlayoutCheckouts);
    		
    		// build new control to be added
    		Button newCheckout = new Button(this);
    		newCheckout.setId(checkoutArray.size());
    		newCheckout.setText(txtCheckout.getText());
			newCheckout.getBackground().setColorFilter(0xFFAA0000, PorterDuff.Mode.MULTIPLY);
			newCheckout.setOnClickListener(OnCheckoutButtonsClick);
			
    		// add to lin layout group
    		checkoutGroup.addView(newCheckout);
    		
    		// clear the checkout name editbox
    		txtCheckout.setText("");
    		
    		// add to our checkout list
    		checkoutArray.add(strCheckout);
    		
    		break;
    		
    	case R.id.btnStartAudit:
    		
    		TextView timeDisplay = (TextView) findViewById(R.id.txtTime);
    		TextView storeName = (TextView) findViewById(R.id.txtStoreName);
    		
    		finish();
    		
    		Intent intent = new Intent(this, Audit.class);
    		intent.putExtra("storeName", storeName.getText().toString());
    		intent.putExtra("startTime", timeDisplay.getText().toString());
    		intent.putStringArrayListExtra("checkouts", checkoutArray);
        
    		startActivity(intent);
    		
    		break;
    		
    	case R.id.btnPickTime:
    		
    		showDialog(TIME_DIALOG_ID);
    		
    		break;
    	}
    		
    }
    
  //------------------------------------------------------
    
    private View.OnClickListener OnCheckoutButtonsClick = new View.OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
        	Button btnCheckout = (Button)v;
        	
        	String strCheckoutName = btnCheckout.getText().toString();
        	checkoutArray.remove(strCheckoutName);
        	
        }
    };
    
	//------------------------------------------------------
    
    // updates the time we display in the TextView
    private void updateDisplay() 
    {
    	TextView timeDisplay = (TextView) findViewById(R.id.txtTime);
    	timeDisplay.setText(
            new StringBuilder()
                    .append(pad(mHour)).append(":")
                    .append(pad(mMinute)));
    }

	//------------------------------------------------------
    
    private static String pad(int c) 
    {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

	//------------------------------------------------------
    
    // the callback received when the user "sets" the time in the dialog
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;
                updateDisplay();
            }
        };
        
    //------------------------------------------------------        
        
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeSetListener, mHour, mMinute, false);
        }
        return null;
    }
    
	//------------------------------------------------------
    
    
}
